package com.example.demo.AppUtil;

import com.example.demo.entity.AnToken;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author:大力王
 * @Date:2024/6/19-19-23:02
 * 开始了
 */
public class TokenSqlProvider {
    public String update(AnToken tokenEntity) {
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE an_token SET ");

        List<String> conditions = new ArrayList<>();
        if (tokenEntity.getToken() != null) {
            conditions.add("`token` = #{token}");
        }
        if (tokenEntity.getExpireTime() != null) {
            conditions.add("`expire_time` = #{expireTime}");
        }
        if (tokenEntity.getUpdateTime() != null) {
            conditions.add("`update_time` = #{updateTime}");
        }

        // 添加条件到SQL字符串，避免最后的逗号问题
        for (int i = 0; i < conditions.size(); i++) {
            sql.append(conditions.get(i));
            if (i < conditions.size() - 1) {
                sql.append(", ");
            }
        }

        sql.append(" WHERE user_id = #{userId}");

        return sql.toString();
    }
}
